package common.proxy;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;

import java.util.ResourceBundle;

public class LoadInfo {
    private static String PROXY_STR = null;
    private static Integer PORT_STR = null;
    private static String USER_NAME_STR = null;
    private static String PASSWORD_STR = null;

    static {
        ResourceBundle proxy = ResourceBundle.getBundle("proxy");
        PROXY_STR = proxy.getString("proxyStr");
        PORT_STR = Integer.valueOf(proxy.getString("portStr"));
        USER_NAME_STR = proxy.getString("userNameStr");
        PASSWORD_STR = proxy.getString("passwordStr");
    }

    //这里创建代理对象
    public static CredentialsProvider build() {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(PROXY_STR, PORT_STR), (Credentials) new UsernamePasswordCredentials(USER_NAME_STR, PASSWORD_STR));
        return credsProvider;
    }


    public static RequestConfig buildRequestConfigureWithProxy(boolean JumpFlag) {
        RequestConfig requestConfig = RequestConfig.custom()
                //没有链接,没有响应超时断开
                .setSocketTimeout(60000)
                //链接后没有响应数据, 超时断开
                .setConnectTimeout(60000)
                .setProxy(new HttpHost(PROXY_STR, PORT_STR))
                .setRedirectsEnabled(JumpFlag)
                .build();// 设置请求和传输超时时间
        return requestConfig;
    }

    public static RequestConfig buildRequestConfigureWithOutProxy(boolean JumpFlag) {
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(60000)
                .setConnectTimeout(60000)
                //.setRedirectsEnabled(JumpFlag)
                .build();// 设置请求和传输超时时间
        return requestConfig;
    }


}
